package com.opencv.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @author qz.wu
 * @date 2022/1/10 11:41
 * @descriptions TODO
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件导入、导出测试")
public class FileController {

    @ApiOperation("上传文件测试")
    @PostMapping(path = "/upload", consumes = "multipart/related; type=\"application/dicom\"", produces = "application/dicom+json")
    // @PostMapping(path = "/upload")
    public String upload(MultipartHttpServletRequest request, @RequestParam(value = "ClinicNumber", required = false) String clinicNumber) {
        System.out.println(clinicNumber);
        MultipartFile file = request.getFile("file");
        File distFile = new File(request.getSession().getServletContext().getRealPath("/") + "template/" + file.getOriginalFilename());
        // 更新文件名
        distFile.renameTo(new File("u" + new Date().toString()));
        try {
            //把文件写到服务器
            file.transferTo(distFile);
        } catch (IllegalStateException | IOException e1) {
            e1.printStackTrace();
        }
        return "success";
    }

    @ApiOperation("上传文件测试Next")
    @PostMapping(path = "/upload/next")
    public String uploadNext(MultipartHttpServletRequest request, @RequestParam(value = "ClinicNumber", required = false) String clinicNumber) {
        System.out.println(clinicNumber);
        MultipartFile file = request.getFile("file");
        File distFile = new File( "D:/template/" + file.getOriginalFilename());
        // 更新文件名
        distFile.renameTo(new File("u" + new Date().toString()));
        try {
            //把文件写到服务器
            file.transferTo(distFile);
        } catch (IllegalStateException | IOException e1) {
            e1.printStackTrace();
        }
        return "success";
    }
}
